﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;

namespace PhoneCard.Controllers
{
    public class AccountController : Controller
    {
        DAL.TuiUsersDal tuiUserDal;
        public AccountController(DAL.TuiUsersDal tuiUserDal)
        {
            this.tuiUserDal = tuiUserDal;
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult AccessDenied()
        {
            return View();
        }
        

        [HttpPost]
        public async Task<IActionResult> Login(string userName, string userPassword,  string returnUrl = "")
        {

            Entities.TuiUsers model = tuiUserDal.GetModelByUserName(userName);
            if (model == null)
            {
                return this.Content(string.Format("<script>alert('用户名或者密码错误');location.href='{0}'</script>", Url.Action("Index", "Account")), "text/html;charset=utf8");
            }
            if (model.userName != userPassword) { 
            
                return this.Content(string.Format("<script>alert('用户名或者密码错误');location.href='{0}'</script>", Url.Action("Index", "Account")), "text/html;charset=utf8");
            }


            int userType = model.userType;
            var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
            claimsIdentity.AddClaim(new Claim(ClaimTypes.Sid, userName));
           
            if (userType == RoleTypeEnum.UserType_Admin)
            {
                //3个权限
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.Admin));
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.User));
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.Company));
            }
            else
            {
                
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.User));
                if(userType==1)
                    claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, RoleTypeEnum.Company));
            }

            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
            });

            if (!string.IsNullOrEmpty(returnUrl))
            {
                return this.Redirect(returnUrl);
            }
            else
            {
                if (userType == RoleTypeEnum.UserType_Admin)
                {
                    //return this.Redirect(Url.Action("Index", "Home", new { area = "Admin" }));
                    return this.Redirect(Url.Action("Index", "Home", new { area = "Admin" }));
                }
                else
                {
                    //return this.Redirect(Url.Action("Index", "Home", new { area = "User" }));
                    return this.Redirect(Url.Action("Index", "User"));
                }
            }

        }

        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return this.Redirect(Url.Action("Index", "Account", new { area = "" }));
        }
    }
}